<?php
class Users_IndexController extends Zend_Controller_Action {
	/**
	* По идее здесь должна быть страница полного профиля пользователя
	*/
	public function indexAction() {
		$username = $this->_request->getParam('username','');
		if($username == '') {
			//404 ошибка
			throw new Zend_Controller_Action_Exception($this->view->translate('User not found'), 404);
		}
		
		$user = Doctrine_Core::getTable('Users_Model_Users')->findOneByUsername($username);
		if(!$user) {
			//404 ошибка
			throw new Zend_Controller_Action_Exception($this->view->translate('User not found'), 404);
		}
		
		
		$this->view->user = $user;
		
		$auth = Zend_Auth::getInstance();
		
		if($auth->hasIdentity() && $auth->getIdentity()->id == $user->id) {
			//просмариваем свой профиль
			$this->render('own');
		} else {
			$this->render('index');
		}
	}
	
	/**
	* Форма отправки сообщения пользователю
	*/
	public function sendAction() {
		$auth = Zend_Auth::getInstance();
		
		if($auth->hasIdentity()) {
			$own = $auth->getIdentity();
			//получаем имя пользователя
			$username = $this->_request->getParam('username',null);
			if(!$username) {
				//404 ошибка
				throw new Zend_Controller_Action_Exception($this->view->translate('User not found'), 404);
			}
			
			$user = Doctrine_Query::create()
					->from('Users_Model_Users')
					->where('username = ?',$username)
					//->andWhere('delete = ?', "no")
					->execute();
		
			if($user->count() != 1) {
				//404 ошибка
				throw new Zend_Controller_Action_Exception($this->view->translate('User not found'), 404);
			}
			
			$form = new Users_Form_Send();
			$form->setAction($this->view->url(array('username' => $username),'users_index_send'));
			
			if($this->_request->isPost() && $form->isValid($this->_request->getPost())) {
				$values = $form->getValues();
				
				$msg = new Users_Model_Messages();
				$msg->to_id = $user[0]->id;
				$msg->from_id = $own->id;
				$msg->created_at = time();
				$msg->is_read = 0;
				$msg->message = $values['message'];
				$msg->save();
				
				$this->_helper
					->getHelper('FlashMessenger')
					->addMessage($this->view->translate('Done! Message sent successfully!'));
				$this->_redirect($this->view->url(array('username' => $username),'users_index_send'));
			}
			
			$this->view->username = $username;
			$this->view->form = $form;
		} else {
			$this->_redirect($this->view->url(array(),'default_auth_login'));
		}
	}
	
	/**
	* Просмотр входящих сообщений
	*/
	public function messagesAction() {
		$auth = Zend_Auth::getInstance();
		
		if($auth->hasIdentity()) {
			$user = $auth->getIdentity();
		
			$messages = Doctrine_Query::create()
						->from('Users_Model_Messages')
						->where('to_id = ?', $user->id)
						->orderBy('is_read ASC')
						->addOrderBy('created_at DESC')
						->execute();
						
			$this->view->messages = $messages;
						
		} else {
			$this->_redirect($this->view->url(array(),'default_auth_login'));
		}
	}
	
	
}